package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");  // wht6.cn
        // f.setPort(5672); //默认端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        // 1.定义队列  2.定义交换机  3.绑定
        String queue = UUID.randomUUID().toString(); //随机队列名
        c.queueDeclare(queue,false,true,true,null);
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        // 对于fanout类型交换机，第三个参数无效
        c.queueBind(queue, "logs", "");

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到： "+s);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };

        // 消费数据
        c.basicConsume(queue, true, deliverCallback, cancelCallback);
    }
}
